using System;
using System.IO;
using System.Text;
using System.Diagnostics;

using A4G.Core;
using A4G.Core.Algorithms;

namespace A4G.Assignment2
{
    public class Program
    {
        static void PrintHelp()
        {
            Console.WriteLine("Algorithm For Genomes Assignment 2.");
            Console.WriteLine("Start as follows:");
            Console.WriteLine("a4g.assignment2 <filename1> <filename2>");
            Console.WriteLine("Where <filename1> and <filename2> are the fasta files containing the circular bacterial DNA sequences.");
        }

        static void Main(string[] args)
        {
            ScoringMatrix scoringMatrix = new ScoringMatrix(new char[] { 'A', 'C', 'G', 'T' });
            scoringMatrix.SetAsIdentityMatrix();
            scoringMatrix.SetNonDiagonalValues(-1.0);

            Debug.Assert(args.Length >= 2, "Insufficient program arguments");

            string filename1 = args[0];
            if (!File.Exists(filename1))
            {
                Console.WriteLine("Filename incorrect.");
                Console.WriteLine();
                PrintHelp();
                return;
            }

            string filename2 = args[1];
            if (!File.Exists(filename2))
            {
                Console.WriteLine("Filename incorrect.");
                Console.WriteLine();
                PrintHelp();
                return;
            }

            int maxThreads = 4;
            bool maxThreadsProvided = false;
            if (args.Length > 2)
            {
                maxThreadsProvided = int.TryParse(args[2], out maxThreads);
            }

            FastaFile inputFile1 = new FastaFile(filename1);
            inputFile1.Load();

            FastaFile inputFile2 = new FastaFile(filename2);
            inputFile2.Load();

            HirschbergAlignment alignment = new HirschbergAlignment(
                ((Sequence)inputFile1.Sequences[0]).SequenceString,
                ((Sequence)inputFile2.Sequences[0]).SequenceString,
                scoringMatrix,
                -3.0);

            AlignmentPath path = alignment.GetAlignmentPath();
            path.WriteToFile(Path.ChangeExtension(filename1, "a4g.output"));
        }
    }
}
